﻿using OLMS.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OLMS_MVC2.Models;

namespace OLMS_MVC2.BLL
{
    public class OrderDetailBLL
    {
        public List<OrderSummaryViewModel> SelectAllOrderMethod()
        {
            using (OLMSDBEntities db = new OLMSDBEntities())
            {
                // 按AlbumId分组，计算每个专辑的总销售额并排序
                var albumSales = db.OrderDetails
                    .GroupBy(od => od.AlbumId)
                    .Select(g => new OrderSummaryViewModel
                    {
                        AlbumId = g.Key,
                        TotalQuantity = g.Sum(od => od.Quantity),
                        TotalRevenue = g.Sum(od => od.Quantity * od.UnitPrice),
                        Orders = g.ToList()
                    })
                    .OrderByDescending(a => a.TotalRevenue)
                    .ToList();

                return albumSales;
            }
        }
    }
}